package com.onewayactivity.org;

import java.text.DecimalFormat;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class OneWayActivityActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);       
        findViews();
        restorePrefs();
        setListener();
       // Log.v("VERBOSE","OnCreate");
       // Log.d("DEBUG", "OnCreate");
       // Log.i("INFO", "OnCreate");
       // Log.w("WARNING", "OnCreate");
       // Log.e("ERROR", "OnCreate");
        Log.d("BMI", "OneWayActivityActivity OnCreate");       
    }
    
    @Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		Log.d("BMI", "OneWayActivityActivity onDestroy");
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		Log.d("BMI", "OneWayActivityActivity onPause");
	}

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
		Log.d("BMI", "OneWayActivityActivity onRestart");
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		Log.d("BMI", "OneWayActivityActivity onResume");
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		Log.d("BMI", "OneWayActivityActivity onStart");
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		Log.d("BMI", "OneWayActivityActivity onStop");
		SharedPreferences setting = getSharedPreferences(PREF, 0);
		setting.edit().putString(PREF_HEIGHT, field_height.getText().toString()).commit();
	}

	private void setListener()
    {
    	button_calc.setOnClickListener(calcBMI);
    }
    
    private void findViews()
    {
    	button_calc = (Button)findViewById(R.id.submit);
    	field_weight = (EditText)findViewById(R.id.weight);
    	field_height = (EditText)findViewById(R.id.height);
    	view_result = (TextView)findViewById(R.id.resualt);
    	view_suggest = (TextView)findViewById(R.id.suggest);
    }
    
    private void restorePrefs()
    {
    	SharedPreferences setting = getSharedPreferences(PREF, 0);
    	String pref_height = setting.getString(PREF_HEIGHT, "");
    	if(pref_height != ""){
    		field_height.setText(pref_height);
    		field_weight.requestFocus();
    	}
    }
    
	private Button button_calc;
    private EditText field_weight;
    private EditText field_height;
    private TextView view_result;
    private TextView view_suggest;
    private static final String TAG = "Bmi";
    private static final String PREF = "BMI_PREF";
    private static final String PREF_HEIGHT = "BMI_HEIGHT";
    
    private Button.OnClickListener calcBMI = new Button.OnClickListener(){

		@Override
		public void onClick(View v) {
			Intent intent = new Intent();
			intent.setClass(v.getContext(), Report.class);
			Bundle bundle = new Bundle();
			bundle.putString("KEY_HEIGHT", field_height.getText().toString());
			bundle.putString("KEY_WEIGHT", field_weight.getText().toString());
			intent.putExtras(bundle);
			startActivity(intent);	
		}
	};
}